package com.atguigu.guli.service.edu.controller.admin;

import com.atguigu.guli.common.base.result.R;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.vo.CoursePublishVo;
import com.atguigu.guli.service.edu.entity.vo.CourseQueryVo;
import com.atguigu.guli.service.edu.entity.vo.CourseVo;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @className: CourseController
 * @description: 课程前端控制器
 * @date: 2020/12/5
 * @author: cakin
 */
@CrossOrigin //允许跨域
@Api(description = "课程管理")
@RestController
@RequestMapping("/admin/edu/course")
@Slf4j
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 功能描述：新增课程
     *
     * @param courseInfoForm 前端传过来的DTO
     * @return R 返回给前端数据
     * @author cakin
     * @date 2020/12/5
     */
    @ApiOperation("新增课程")
    @PostMapping("save-course-info")
    public R saveCourseInfo(@ApiParam(value = "课程基本信息", required = true) @RequestBody CourseInfoForm courseInfoForm) {
        String courseId = courseService.saveCourseInfo(courseInfoForm);
        return R.ok().data("courseId", courseId).message("保存成功");
    }

    /**
     * 功能描述：根据ID查询课程
     *
     * @param id 课程id
     * @return R 返回给前端数据
     * @author cakin
     * @date 2020/12/5
     */
    @ApiOperation("根据ID查询课程详情")
    @GetMapping("course-info/{id}")
    public R getById(@ApiParam(value = "课程ID", required = true) @PathVariable String id) {
        CourseInfoForm courseInfoForm = courseService.getCourseInfoById(id);
        if (courseInfoForm != null) {
            return R.ok().data("item", courseInfoForm);
        } else {
            return R.ok().message("数据不存在");
        }
    }

    /**
     * 功能描述：更新课程
     *
     * @param courseInfoForm 前端传过来的DTO
     * @return R 返回给前端数据
     * @author cakin
     * @date 2020/12/5
     */
    @ApiOperation("更新课程")
    @PutMapping("update-course-info")
    public R updateCourseInfoById(@ApiParam(value = "课程ID", required = true) @RequestBody CourseInfoForm courseInfoForm) {
        courseService.updateCourseInfoById(courseInfoForm);
        return R.ok().message("修改成功");
    }

    /**
     * 功能描述：课程分页列表
     *
     * @param page  当前页码
     * @param limit 每页记录数
     * @return R 返回给前端数据
     * @author cakin
     * @date 2020/12/6
     */
    @ApiOperation("课程分页列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
                      @ApiParam("课程列表查询对象") CourseQueryVo courseQueryVo) {
        IPage<CourseVo> pageModel = courseService.selectPage(page, limit, courseQueryVo);
        List<CourseVo> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 功能描述：根据ID删除课程
     *
     * @param id 课程id
     * @return R 返回给前端的数据
     * @author cakin
     * @date 2020/12/6
     */
    @ApiOperation("根据ID删除课程")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "课程id", required = true) @PathVariable String id) {
        // TODO: 删除课程视频
        // 此处调用vod中的删除视频文件的接口
        // 删除课程封面
        courseService.removeCoverById(id);
        // 删除课程
        boolean result = courseService.removeCourseById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    /**
     * 功能描述：根据ID获取课程发布信息
     *
     * @param id 课程id
     * @return R 返回给前端数据
     * @author cakin
     * @date 2020/12/6
     */
    @ApiOperation("根据ID获取课程发布信息")
    @GetMapping("course-publish/{id}")
    public R getCoursePublishVoById(@ApiParam(value = "课程ID", required = true) @PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.getCoursePublishVoById(id);
        if (coursePublishVo != null) {
            return R.ok().data("item", coursePublishVo);
        } else {
            return R.error().message("数据不存在");
        }
    }

    /**
     * 功能描述：根据id发布课程
     *
     * @param id 课程id
     * @return R 返回前端数据
     * @author cakin
     * @date 2020/12/6
     */
    @ApiOperation("根据id发布课程")
    @PutMapping("publish-course/{id}")
    public R publishCourseById(@ApiParam(value = "课程ID", required = true) @PathVariable String id) {
        boolean result = courseService.publishCourseById(id);
        if (result) {
            return R.ok().message("发布成功");
        } else {
            return R.error().message("数据不存在");
        }
    }
}

